<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <Treetools :tree-data="company" :is-root="true" @openDialog="openDialog" />

        <el-tree :data="data" :props="{label:'name',children:'children'}">
          <Treetools slot-scope="{ data }" :tree-data="data" @updateList="initData" @openDialog="openDialog" @editDialog="editDialog" />
        </el-tree>
      </el-card>
    </div>
    <!-- 修改、添加弹窗 -->
    <AddDept ref="addDept" @updateList="initData" />
  </div>
</template>

<script>
import { getDepartmentListApi } from '@/api/department'
import Treetools from '@/views/departments/components/tree-tools.vue'
import AddDept from '@/views/departments/components/add-dept.vue'
import { tranListTreeToData } from '@/utils/index'
export default {
  components: { Treetools, AddDept },

  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      data: []

    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const { companyName, depts } = await getDepartmentListApi()
      // console.log(depts)
      this.company.name = companyName
      this.data = tranListTreeToData(depts, '')
      // console.log(this.data)
    },
    openDialog(val) {
      // console.log(this.$refs.addDept) //获取到组件的实例对象
      this.$refs.addDept.dialogShow = true
      this.$refs.addDept.formData.pid = val
    },
    editDialog(val) {
      this.$refs.addDept.getFormDataInfo(val)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
